package com.athenaeum.datastore.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import org.springframework.format.annotation.DateTimeFormat;

import com.athenaeum.datastore.util.UserUtil;

@MappedSuperclass
public abstract class AbstractUserAuditFields {

	@DateTimeFormat(pattern = "dd/MM/yyyy")
	@Column(name = "createdOn", insertable = true, updatable = false)
	private Date createdOn;
	@Column(name = "createdBy", insertable = true, updatable = false)
	private String createdBy;
	@DateTimeFormat(pattern = "dd/MM/yyyy")
	@Column(name = "updatedOn", insertable = false, updatable = true)
	private Date updatedOn;
	@Column(name = "updatedBy", insertable = false, updatable = true)
	private String updatedBy;

	@PrePersist
	protected void onCreate() {
		this.updatedOn = this.createdOn = new Date();
		this.updatedBy = this.createdBy = UserUtil.getUsernameInContext();
	}

	@PreUpdate
	protected void onUpdate() {
		this.updatedOn = new Date();
		this.updatedBy = UserUtil.getUsernameInContext();
	}

	public Date getCreatedOn() {
		return createdOn;
	}

	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Date getUpdatedOn() {
		return updatedOn;
	}

	public void setUpdatedOn(Date updatedOn) {
		this.updatedOn = updatedOn;
	}

	public String getUpdatedBy() {
		return updatedBy;
	}

	public void setUpdatedBy(String updatedBy) {
		this.updatedBy = updatedBy;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((createdBy == null) ? 0 : createdBy.hashCode());
		result = prime * result
				+ ((createdOn == null) ? 0 : createdOn.hashCode());
		result = prime * result
				+ ((updatedBy == null) ? 0 : updatedBy.hashCode());
		result = prime * result
				+ ((updatedOn == null) ? 0 : updatedOn.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		AbstractUserAuditFields other = (AbstractUserAuditFields) obj;
		if (createdBy == null) {
			if (other.createdBy != null)
				return false;
		} else if (!createdBy.equals(other.createdBy))
			return false;
		if (createdOn == null) {
			if (other.createdOn != null)
				return false;
		} else if (!createdOn.equals(other.createdOn))
			return false;
		if (updatedBy == null) {
			if (other.updatedBy != null)
				return false;
		} else if (!updatedBy.equals(other.updatedBy))
			return false;
		if (updatedOn == null) {
			if (other.updatedOn != null)
				return false;
		} else if (!updatedOn.equals(other.updatedOn))
			return false;
		return true;
	}

}
